/*
 * ImportSources module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.importsources;

import org.openide.WizardDescriptor;

import rogue.netbeans.module.importsources.ui.WizardPanel1UI;

import java.awt.Component;

import java.io.File;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * @author Rogue
 */
public class WizardPanel1 extends AbstractWizardPanel
    implements WizardDescriptor.Panel, DocumentListener {
    private WizardPanel1UI component;
    private WizardDescriptor wd = null;
    private String selectedDirectory = null;

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    public Component getComponent() {
        if (component == null) {
            component = new WizardPanel1UI(this);
        }

        return component;
    }

    public boolean isValid() {
        if ((selectedDirectory == null) || (selectedDirectory.length() == 0)) {
            if (wd != null) {
                wd.putProperty("WizardPanel_errorMessage",
                               "Specify a valid directory.");
            }

            return false;
        }

        File f = new File(selectedDirectory);

        if (!f.isDirectory()) {
            if (wd != null) {
                wd.putProperty("WizardPanel_errorMessage",
                               "Specify a valid directory.");
            }

            return false;
        }

        if (wd != null) {
            wd.putProperty("WizardPanel_errorMessage", null);
        }

        return true;
    }

    public void readSettings(Object settings) {
        if (settings instanceof WizardDescriptor) {
            wd = (WizardDescriptor) settings;

            // Invoke the validity to specify the error message.
            fireChangeEvent();
        }
    }

    public void storeSettings(Object settings) {
        wd.putProperty(ImportSourcesConstants.SELECTED_DIRECTORY.toString(),
                       selectedDirectory);
        wd.putProperty(ImportSourcesConstants.FILE_EXTENSIONS.toString(),
                       component.getFileExensions());
        wd.putProperty(ImportSourcesConstants.SCAN_FOR_FILES.toString(),
                       Boolean.TRUE);
    }

    //
    // Document listener methods.
    //
    public void insertUpdate(DocumentEvent e) {
        selectedDirectory = component.getSelectedDirectory();
        fireChangeEvent();
    }

    public void removeUpdate(DocumentEvent e) {
        selectedDirectory = component.getSelectedDirectory();
        fireChangeEvent();
    }

    public void changedUpdate(DocumentEvent e) {
        // safely ignore.
    }

    public boolean isPanelProcessingComplete() {
        return true;
    }

    public void terminateProcessing() {
        // NO-OP
    }
}
